package com.duowei.kitchenshow.fragment.setting;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckedTextView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.duowei.kitchenshow.R;
import com.duowei.kitchenshow.adapter.SpacesItemDecoration;
import com.duowei.kitchenshow.bean.Kitchens;
import com.duowei.kitchenshow.httputils.DownHTTP;
import com.duowei.kitchenshow.httputils.VolleyResultListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.litepal.crud.DataSupport;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class KitchenSelectFragment extends DialogFragment implements View.OnClickListener{
    private List<Kitchens> mKitchensList=new ArrayList<>();
    private KitchensAdapter mKitchensAdapter;
    private ProgressBar mPb;

    public KitchenSelectFragment() {
        // Required empty public constructor
    }

    public interface KitchenSelect{
        void onKitchenSelect(List<Kitchens> kitchensList);
    }
    private KitchenSelect mListener;
    public void setOnKitchenSelectListener(KitchenSelect listener){
        mListener=listener;
    }

    public static KitchenSelectFragment newInstance(String ip) {
        Bundle args = new Bundle();
        args.putString("ip",ip);
        KitchenSelectFragment fragment = new KitchenSelectFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_kitchen_select, container, false);
        String ip = getArguments().getString("ip");


        RecyclerView rv = inflate.findViewById(R.id.recycleView);
        mPb = inflate.findViewById(R.id.progressBar);
        inflate.findViewById(R.id.btn_confirm).setOnClickListener(this);
        inflate.findViewById(R.id.btn_cancel).setOnClickListener(this);

        rv.setLayoutManager(new GridLayoutManager(getActivity(),3));
        rv.addItemDecoration(new SpacesItemDecoration(10));
        mKitchensAdapter = new KitchensAdapter(mKitchensList);
        rv.setAdapter(mKitchensAdapter);

        Http_kitchens(ip);
        return inflate;
    }

    private void Http_kitchens(String ip) {
        String url="http://" + ip + ":2233/server/ServerSvlt?";
        String sql="select cbbh,cbmc,bcpjsjm from bmsz where isnull(sfty,'0')<>'1' and isnull(sfbcp,'0')='1'|";
        DownHTTP.postVolley6(url, sql, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(),"网络连接失败", Toast.LENGTH_SHORT).show();
                mPb.setVisibility(View.GONE);
            }
            @Override
            public void onResponse(String response) {
                if(response.equals("]")||response.toUpperCase().contains("ERROR")){
                    Toast.makeText(getActivity(),"查无厨房屏信息，请到门店管理设置", Toast.LENGTH_SHORT).show();
                }else{
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Kitchens>>() {
                    }.getType();
                    mKitchensList = gson.fromJson(response, type);
                    List<Kitchens> kitchens = DataSupport.findAll(Kitchens.class);
                    for(int i=0;i<mKitchensList.size();i++){
                        Kitchens kitchens1 = mKitchensList.get(i);
                        for(int j=0;j<kitchens.size();j++){
                            if(kitchens1.getCbbh().equals(kitchens.get(j).getCbbh())){
                                kitchens1.setSelect(kitchens.get(j).isSelect());
                                break;
                            }
                        }
                    }
                    mKitchensAdapter.setNewData(mKitchensList);
                }
                mPb.setVisibility(View.GONE);
            }
        });
    }

    class KitchensAdapter extends BaseQuickAdapter<Kitchens> {
        public KitchensAdapter(List<Kitchens> data) {
            super(R.layout.kitchen_item, data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, final Kitchens kitchens) {
            baseViewHolder.setText(R.id.text,kitchens.getCbmc());
            final CheckedTextView ctv = baseViewHolder.getView(R.id.text);
            ctv.setChecked(kitchens.isSelect());
            ctv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    kitchens.setSelect(!kitchens.isSelect());
                    ctv.setChecked(kitchens.isSelect());
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.btn_confirm){
            DataSupport.deleteAll(Kitchens.class);
            DataSupport.saveAll(mKitchensList);
            mListener.onKitchenSelect(mKitchensList);
            dismiss();
        }else if(view.getId()==R.id.btn_cancel){
            dismiss();
        }
    }
}
